
package com.ukao.cashregister.pesenter;

import android.content.Context;
import android.util.Log;

import com.ukao.cashregister.base.BasePresenter;
import com.ukao.cashregister.bean.StringBean;
import com.ukao.cashregister.bean.VipGroupBean;
import com.ukao.cashregister.bean.VipLeverBean;
import com.ukao.cashregister.consts.Constant;
import com.ukao.cashregister.consts.SaveParamets;
import com.ukao.cashregister.retrofit.ApiCallback;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.Utils;
import com.ukao.cashregister.view.CreateVipView;

import java.util.HashMap;

/**
 * Created by ukaodev on 2017/6/28.
 */

public class BuiltVipPesenter extends BasePresenter<CreateVipView> {

    public BuiltVipPesenter(CreateVipView view) {
        attachView(view);
    }


    /**
     *用戶等級
     */
    public void vipUserLever( Context mContext  ) {
        mvpView.showLoading();
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("accessToken", SaveParamets.getToken(mContext));
        addSubscription(apiStores.vipUserLever( Constant.createParameter(mHashMap)),
                new ApiCallback<VipLeverBean>() {
                    @Override
                    public void onSuccess(VipLeverBean model) {
                        if (model.getHttpCode()==200){
                            VipLeverBean.ListBean item=new VipLeverBean.ListBean();
                            item.setName("所有等级");
                            item.setId("");
                            model.getData().getList().add(0,item);
                            mvpView.loadVipLeverSucceed(model.getData().getList());
                        }else{
                            mvpView.loadFailure(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {
                        requestAllDefaultGroup();
                    }

                });
    }

    /**
     *获取全部分组与默认分组
     */
    public void requestAllDefaultGroup(   ) {
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.alldefauldGroup(Constant.createParameter(mHashMap)),
                new ApiCallback<VipGroupBean>() {
                    public void onSuccess(VipGroupBean model) {

                        if (model.getHttpCode()==200){
                            if (!CheckUtils.isEmpty(model.getData()))

                            mvpView.loadAllPacketSucceed(model.getData());
                        }else{

                            mvpView.loadFailure(model.getMsg());
                        }
                        vipUserGroupData();
                    }



                    @Override
                    public void onFinish() {

                    }

                });
    }


    /**
     *用戶分組
     */
    public void vipUserGroupData(   ) {
        HashMap mHashMap=new HashMap<String,String>();
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores.vipUserGroup(Constant.createParameter(mHashMap)),
                new ApiCallback<VipGroupBean>() {
                    @Override
                    public void onSuccess(VipGroupBean model) {

                        if (model.getHttpCode()==200){
                            if (!CheckUtils.isEmpty(model.getData()))
                                mvpView.loadvipGroupSucceed( model.getData());

                        }else{

                            mvpView.loadFailure(model.getMsg());
                        }

                    }



                    @Override
                    public void onFinish() {
                    mvpView.hideLoading();
                    }

                });
    }


    /**
     * 保存信息
     * @param mHashMap
     */
    public void saveViPName(   HashMap mHashMap) {
        mvpView.showLoading();
        addSubscription(apiStores.buildMember(Constant.createParameter(mHashMap)),
                new ApiCallback<StringBean>() {
                    @Override
                    public void onSuccess(StringBean model) {
                        mvpView.hideLoading();
                        if (model.getHttpCode() == 200) {
                            mvpView.buildUserSucceed();

                        } else {
                            mvpView.loadFailure(model.getMsg());
                        }

                    }


                    @Override
                    public void onFinish() {
                        mvpView.hideLoading();

                    }
                });


    }



}
